<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2017/11/23
 * Time: 下午1:28
 */
namespace app\common\model;
include_once EXTEND_PATH."alipay/AopSdk.php";
use think\Cache;
use think\Model;
use think\Loader;
use app\common\model\Order;
use app\common\model\DeveloperException;
class AliPay extends Common
{
    static public function aliResponse($body, $orderSn, $price)
    {
        $strPublic= config('alipay.alipayrsaPublicKey');
        $strPublic         = chunk_split($strPublic, 64, "\n");
        $public_key = "-----BEGIN PUBLIC KEY-----\n$strPublic-----END PUBLIC KEY-----\n";
        $c = new \AopClient();
        $c->gatewayUrl = config('alipay.gatewayUrl');//请求的网关地址
        $c->appId = config('alipay.app_id');//支付宝分配给开发者的应用ID
        //商户私钥
        $c->rsaPrivateKey = config('alipay.rsaPrivateKey');//私钥
        $c->format = "json";
        $c->charset= "UTF-8";
        $c->signType= "RSA2";//加密方式
        $c->alipayrsaPublicKey = $public_key;//商户公钥
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.open.public.template.message.industry.modify

        $request = new \AlipayTradeAppPayRequest();
        $request->setNotifyUrl(config('alipay.notify_url'));//回调地址
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $request->setBizContent( "{" .
            "    \"subject\":\"$body\"," .
            "    \"out_trade_no\":\"$orderSn\"," .
            "    \"total_amount\":\"$price\"," .
            "    \"product_code\":\"QUICK_MSECURITY_PAY\"" .
            " }");
        $response= $c->sdkExecute($request);//这里就是sdk自己的方法
        return $response;
    }
    /**
     * 查询订单状态
     * @param $transaction_id
     * @return string
     */
    public function getPayStatus($transaction_id)
    {
// 顶部已引入           require_once EXTEND_PATH."alipay/AopSdk.php";
            require_once EXTEND_PATH."alipay/aop/AopClient.php";
            require_once EXTEND_PATH."alipay/aop/request/AlipayTradeQueryRequest.php";
            $aop = new \AopClient ();
            $aop->gatewayUrl = config('alipay.gatewayUrl');
            $aop->appId = config('alipay.app_id');
            $aop->rsaPrivateKey = config('alipay.rsaPrivateKey');
            $aop->alipayrsaPublicKey= config('alipay.alipayrsaPublicKey');
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset='UTF-8';
            $aop->format='json';
            $request = new \AlipayTradeQueryRequest ();
            $request->setBizContent("{" .
            "\"out_trade_no\":\"$transaction_id\"}");
            $result = $aop->execute ( $request);
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            if(!empty($resultCode)&&$resultCode == 10000)
            {
                $status=1;
            }else{
                $status=0;
            }
            return $status;
    }
}